//
//  BaseTableViewController.m
//  Zhiyun
//
//  Created by Leon on 15/10/14.
//  Copyright © 2015年 上海指旺信息科技有限公司. All rights reserved.
//

#import "BaseTableViewController.h"


#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

@interface BaseTableViewController ()

@property (nonatomic, strong) UIView *emptyView;

@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;
@property (nonatomic, strong) MJRefreshAutoNormalFooter *nilFooter;

@end

@implementation BaseTableViewController

- (UIView *)emptyView{
    if (!_emptyView) {
        
        UIView *view =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight / 3)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight / 3)];
        label.text = self.emptyNotice ? self.emptyNotice : @"暂无数据";
        label.font = [UIFont boldSystemFontOfSize:20.0f];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor lightGrayColor];
        [view addSubview:label];
        _emptyView = view;
    }
    return _emptyView;
}

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataCount = 0;
    self.page = 1;
    self.size = 20;
    
    [self addObserver:self forKeyPath:@"dataCount" options:NSKeyValueChangeInsertion | NSKeyValueChangeRemoval | NSKeyValueChangeReplacement |NSKeyValueChangeReplacement context:nil];
    [self addObserver:self forKeyPath:@"pageCount" options:NSKeyValueChangeInsertion | NSKeyValueChangeRemoval | NSKeyValueChangeReplacement |NSKeyValueChangeReplacement context:nil];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:kRefreshTableViewNotification object:nil];

    self.tableView.tableFooterView = [[UIView alloc] init];
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    // 马上进入刷新状态
    [header beginRefreshing];
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.footer = footer;
    // 禁止自动加载
//    footer.automaticallyRefresh = NO;
    // 设置header
    self.tableView.mj_header = header;
    // 设置footer
//    self.tableView.footer = footer;

}

//- (void)refreshData {
//    [self.tableView.header beginRefreshing];
//}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"dataCount"]) {
        
        if (_dataCount == 0) {
            self.tableView.tableFooterView = self.emptyView;
        } else {
            self.tableView.tableFooterView = [[UIView alloc] init];
        }
        
        [self.tableView reloadData];
    } else if([keyPath isEqualToString:@"pageCount"]) {
        if (self.pageCount < self.size) {
            [self.tableView.mj_footer endRefreshing];
            self.tableView.mj_footer = self.nilFooter;
        } else {
            self.tableView.mj_footer = self.footer;
        }
    }
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


//-(void)viewDidLayoutSubviews
//{
//    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
//        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
//    }
//    
//    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
//        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
//    }
//}
//
//-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
//        [cell setSeparatorInset:UIEdgeInsetsZero];
//    }
//    
//    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
//        [cell setLayoutMargins:UIEdgeInsetsZero];
//    }
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    if (tableView.style == UITableViewStyleGrouped)
//        return 5;
//    return 0;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    if (tableView.style == UITableViewStyleGrouped)
//        return 5;
//    return 0;
//}

- (void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self removeObserver:self forKeyPath:@"dataCount"];
    [self removeObserver:self forKeyPath:@"pageCount"];
}

@end
